/**
 * API接口统一管理
 */
import request from "./request.js";
import { getToken, getCurrentUser } from "./auth.js";

// 获取用户信息
const getUserInfo = () => {
  return getCurrentUser() || {};
};

/**
 * 通用接口 - 避免重复定义
 */
export const commonApi = {
  // 获取所有地区数据
  getAllRegion: () => {
    return request.request({
      url: "home/common/get_all_region",
      datas: {
        grts: new Date().getTime() / 1000,
      },
      showLog: false,
    });
  },

  // 获取身份认证信息
  getIdentityInfo: () => {
    return request.request({
      url: "home/Apply/get_identity_info",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 检查支付密码是否已设置
  checkPayPassword: () => {
    return request.request({
      url: "home/pay/check_pwd",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 设置支付密码
  setPayPassword: (data) => {
    return request.request({
      url: "home/pay/add_pwd",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 验证支付密码
  verifyPayPassword: (data) => {
    return request.request({
      url: "home/pay/verify_pwd",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 修改支付密码
  changePayPassword: (data) => {
    return request.request({
      url: "home/pay/update_pwd",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 忘记支付密码 - 发送验证码
  sendPayPasswordCode: (data) => {
    return request.request({
      url: "home/index/gen_mobile_code",
      datas: {
        mobile: data.mobile,
        last_login_ip: data.last_login_ip || "0.0.0.0",
        salt: "1234"
      },
      showLog: true,
    });
  },

  // 忘记支付密码 - 重置密码
  resetPayPassword: (data) => {
    return request.request({
      url: "home/pay/reset_pwd",
      datas: {
        mobile: data.mobile,
        code: data.code,
        password: data.password,
        confirm_password: data.confirm_password,
        last_login_ip: data.last_login_ip || "0.0.0.0",
        salt: "1234"
      },
      token: getToken(),
      showLog: true,
    });
  },
};

/**
 * 微信相关接口
 */
export const wechatApi = {
  // 检查微信Token
  checkToken: () => {
    return request.request({
      url: "wap/wx_help/checkToken",
      datas: {},
      showLog: true,
    });
  },

  // 获取微信配置
  getWxConfig: (data) => {
    return request.request({
      url: "wap/wx_help/getWxConfig",
      datas: data,
      showLog: true,
    });
  },

  // 微信公众号授权登录
  wxLogin: (data) => {
    return request.request({
      url: "home/index/wx_login",
      datas: data,
      showLog: true,
    });
  },
  
  //微信小程序授权登录
  wxMiniLogin: (data) => {
    // 调用正确的接口
    return request.request({
      url: "home/wx_oauth/getOpenidForMiniProgram",
      datas: data,
      showLog: true,
    });
  },

  // 获取微信小程序 openid 和 unionid
  getOpenidForMiniProgram: (data) => {
    return request.request({
      url: "home/wx_oauth/getOpenidForMiniProgram",
      datas: data,
      showLog: true,
    });
  },

  // 通过 code 获取微信小程序用户手机号
  getPhoneNumberForMiniProgram: (data) => {
    return request.request({
      url: "wap/wx_help/getPhoneNumberForMiniProgram",
      datas: {
        code: data.code
      },
      showLog: true,
    });
  },
};

/**
 * 用户相关接口
 */
export const userApi = {
  // 用户登录
  login: (data) => {
    return request.request({
      url: "home/index/login",
      datas: data,
      showLog: false,
    });
  },

  // 获取IP地址
  getIp: () => {
    return request.request({
      url: "wap/wx_help/getip",
      datas: {},
      showLog: true,
    });
  },

  // 检查手机号是否已注册
  checkMobile: (data) => {
    return request.request({
      url: "home/index/check_mobile",
      datas: {
        mobile: data.mobile,
        salt: "1234",
      },
      showLog: true,
    });
  },

  // 用户注册
  register: (data) => {
    // console.log("=== 注册API调用开始 ===");
    // console.log("接收到的原始参数:", JSON.stringify(data, null, 2));

    const requestData = {
      mobile: data.mobile,
      pwd: data.pwd, // 已经是MD5加密的密码
      invite_code: data.invite_code || "",
      region: data.region || 0,
      login_ip: data.login_ip,
      salt: "1234",
      type: data.type || 1,
      longitude: data.longitude || 0,
      latitude: data.latitude || 0,
      unionid: data.unionid || "",
      openid: data.openid || "",
      nickname: data.nickname || "",
      headimgurl: data.headimgurl || "",
      sex: data.sex || "",
    };

    // console.log("最终发送的请求参数:", JSON.stringify(requestData, null, 2));
    // console.log("=== 注册API调用结束 ===");

    return request.request({
      url: "home/index/register",
      datas: requestData,
      showLog: true,
    });
  },

  // 发送验证码
  sendCode: (data) => {
    return request.request({
      url: "home/index/gen_mobile_code",
      datas: {
        mobile: data.mobile,
        salt: "1234",
      },
      showLog: true,
    });
  },

  // 发送手机号验证码（更换手机号）
  sendMobileCodeForChange: (data) => {
    return request.request({
      url: "home/index/gen_mobile_code",
      datas: {
        mobile: data.mobile,
        last_login_ip: data.last_login_ip || "0.0.0.0",
        salt: "1234",
      },
      showLog: true,
    });
  },

  // 验证手机号验证码
  
  // 更换手机号
  changePhone: (data) => {
    return request.request({
      url: "home/user/change_phone",
      datas: {
        mobile: data.mobile,
        code: data.code,
        last_login_ip: data.last_login_ip || "0.0.0.0",
        salt: "1234",
      },
      token: getToken(),
      showLog: true,
    });
  },
  checkMobileCode: (data) => {
    return request.request({
      url: "home/index/check_mobile_code",
      datas: {
        mobile: data.mobile,
        code: data.code,
        last_login_ip: data.last_login_ip || "0.0.0.0",
        salt: "1234",
      },
      showLog: true,
    });
  },

  // 发送手机号验证码（用于绑定）
  sendMobileCode: (data) => {
    return request.request({
      url: "home/wx_user/sendMobileCode",
      datas: {
        mobile: data.mobile,
        salt: "1234",
      },
      showLog: true,
    });
  },

  // 用户手机号绑定
  userPhoneBind: (data) => {
    return request.request({
      url: "home/wx_user/userPhoneBind",
      datas: {
        phone: data.phone,
        code: data.code,
        unionid: data.unionid,
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 获取用户详细信息
  getUserDetail: () => {
    return request.request({
      url: "home/User/get_user_info",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 修改用户信息
  updateUserInfo: (data) => {
    return request.request({
      url: "home/User/update_user_info",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 修改密码
  changePassword: (data) => {
    return request.request({
      url: "home/User/change_password",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 修改登录密码
  updateUserPassword: (data) => {
    return request.request({
      url: "home/user/update_user_pwd",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  //重置密码
  resetAccountPassword: (data) => {
    return request.request({
      url: "home/index/reset_account_pwd",
      datas: data,
      header: {
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
      },
      showLog: true,
    });
  },

  // 绑定手机号
  bindPhone: (data) => {
    return request.request({
      url: "home/User/bind_phone",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 实名认证信息
  getIdentityInfo: () => {
    return request.request({
      url: "home/Apply/get_identity_info",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 提交实名认证（普通用户）
  submitIdentity: (data) => {
    return request.request({
      url: "home/Apply/submit_identity",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 提交实名认证（支持不同类型）
  submitRealIdentity: (data, urlType = 'real_identity') => {
    const url = urlType === 'staff_real_identity' ? "home/Apply/staff_real_identity" : "home/apply/real_identity";
    return request.request({
      url: url,
      datas: {
        realname: data.realname,
        id_number: data.id_number,
        bank_num: data.bank_num,
        bank_name: data.bank_name
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 获取我的页面信息
  getMyInfo: () => {
    const timestamp = Date.now();
    return request.request({
      url: `home/User/my?t=${timestamp}`,
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 获取未登录信息
  getUnlogInfo: () => {
    return request.request({
      url: "home/User/unloginfo",
      datas: {},
      showLog: false,
    });
  },

  // 获取收益信息
  getIncomeInfo: () => {
    return request.request({
      url: "home/User_income/getIncome",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },
};



/**
 * 商品相关接口
 */
export const goodsApi = {
  // 获取商品分类列表
  getCategoryList: () => {
    return request.request({
      url: "home/Goods/catetj",
      datas: {},
      showLog: false,
    });
  },
  // 获取商品分类列表
  getGoodsByCategory: (data) => {
    return request.request({
      url: "home/Goods/catelist",
      datas: data,
      showLog: false,
    });
  },
  // 获取商品列表
  getGoodsList: (data) => {
    const time = new Date().getTime();
    return request.request({
      url: `home/Goods/screenlist?t=${time}`,
      datas: data,
      showLog: false,
    });
  },

  // 获取筛选品牌列表
  getFilterBrands: (data) => {
    const time = new Date().getTime();
    return request.request({
      url: `home/filter/click_filter?t=${time}`,
      datas: data,
      showLog: false,
    });
  },

  // 获取商品详情
  getGoodsDetail: (data) => {
    return request.request({
      url: "home/Goods/goods_xq",
      datas: data,
      showLog: false,
    });
  },

  // 获取商品分类
  getGoodsCategory: () => {
    return request.request({
      url: "home/Goods/get_category",
      datas: {},
      showLog: false,
    });
  },
  // 获取商品规格
  getGoodsSpec: (data) => {
    return request.request({
      url: "home/goods/get_goods_spec",
      datas: data,
      showLog: false,
    });
  },

  // 获取商品评价
  getGoodsComment: (data) => {
    return request.request({
      url: "home/goods/get_goods_comment",
      datas: data,
      showLog: false,
    });
  },

  // 获取商品参数
  getGoodsParam: (data) => {
    return request.request({
      url: "home/goods/get_goods_param",
      datas: data,
      showLog: false,
    });
  },

  // 获取商品规格信息
  getGoodsSpecInfo: (data) => {
    return request.request({
      url: "home/goods/get_goods_spec_info",
      datas: data,
      showLog: false,
    });
  },

  // 搜索商品
  searchGoods: (data) => {
    return request.request({
      url: "home/Goods/search_goods",
      datas: data,
      showLog: false,
    });
  },

  // 获取推荐商品
  getRecommendGoods: (data) => {
    return request.request({
      url: "home/Goods/get_recommend_goods",
      datas: data,
      showLog: false,
    });
  },

  // 获取商品楼层
  getGoodsFloors: () => {
    return request.request({
      url: "home/Goods/get_floors",
      datas: {},
      showLog: false,
    });
  },

  // 获取分类商品
  getCategories: () => {
    return request.request({
      url: "home/Goods/get_categories",
      datas: {},
      showLog: false,
    });
  },
};

/**
 * 购物车相关接口
 */
export const cartApi = {
  // 添加到购物车
  addToCart: (data) => {
    return request.request({
      url: "home/cart/addCart",
      datas: data,
      token: getToken(),
      showLog: false,
    });
  },

  // 获取购物车数量
  getCartCount: () => {
    return request.request({
      url: "home/cart/getCartCount",
      datas: {},
      token: getToken(),
      showLog: false,
    });
  },

  // 获取购物车列表
  getCartList: () => {
    return request.request({
      url: "home/cart/cartlist",
      datas: {},
      token: getToken(),
      showLog: false,
    });
  },

  // 编辑购物车商品（数量、选中状态）
  editCartGoods: (data) => {
    return request.request({
      url: "home/cart/edit_cart_goods",
      datas: {
        cart_id: data.cart_id,
        goods_id: data.goods_id,
        spec_id: data.spec_id,
        num: data.num,
        is_sel: data.is_sel,
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 批量选择购物车商品
  selectCartGoods: (data) => {
    return request.request({
      url: "home/cart/cart_goods_sel",
      datas: {
        cart_id: data.cart_id, // 多个ID用逗号分隔
        is_sel: data.is_sel,
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 删除购物车商品
  deleteCartGoods: (data) => {
    return request.request({
      url: "home/cart/del_cart_goods",
      datas: {
        cart_id: data.cart_id, // 多个ID用逗号分隔
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 更新购物车商品数量
  updateCartNum: (data) => {
    return request.request({
      url: "home/Cart/update_cart_num",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 删除购物车商品
  deleteCartItem: (data) => {
    return request.request({
      url: "home/Cart/delete_cart_item",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 清空购物车
  clearCart: () => {
    return request.request({
      url: "home/Cart/clear_cart",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },
};

/**
 * 订单相关接口
 */
export const orderApi = {
  // 获取订单预览信息（确认订单页面使用）
  getOrderPreview: (data) => {
    return request.request({
      url: "home/order/order_sel",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 提交订单
  submitOrder: (data) => {
    return request.request({
      url: "home/order/order_submit",
      datas: data,
      token: getToken(),
      showLog: false,
    });
  },

  // 创建订单
  createOrder: (data) => {
    return request.request({
      url: "home/Order/create_order",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取订单列表
  getOrderList: (data) => {
    return request.request({
      url: "home/order/get_order_list",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取退换货订单列表
  getOrderRefundList: (data) => {
    return request.request({
      url: "home/refund_record/get_refund_record_lists",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取订单详情
  getOrderDetail: (data) => {
    return request.request({
      url: "home/Order/get_order_detail",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 取消订单
  cancelOrder: (data) => {
    return request.request({
      url: "home/Order/cancel_order",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取物流信息
  getLogisticsInfo: (data) => {
    return request.request({
      url: "home/delivery_info/query_delivery",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 确认收货
  confirmOrder: (data) => {
    return request.request({
      url: "home/Order/confirm_order",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 申请退款
  applyRefund: (data) => {
    return request.request({
      url: "home/Order/apply_refund",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 再次购买（将订单商品加入购物车）
  buyAgain: (data) => {
    return request.request({
      url: "home/Order/buy_again",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取团购订单列表
  getGroupOrderList: (data) => {
    return request.request({
      url: "o2o/my_order/selGroupOrder",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 申请团购订单退款
  refundGroupOrder: (data) => {
    return request.request({
      url: "o2o/my_order/refundGroupOrder",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取待评价商品列表
  getWaitCommentList: (data) => {
    return request.request({
      url: "home/goods_eval/get_wait_comment",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },
};

/**
 * 地址相关接口
 */
export const addressApi = {
  // 获取地址列表
  getAddressList: () => {
    return request.request({
      url: "home/user_address/get_user_address_list",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 添加/更新地址（原系统使用同一个接口）
  addAddress: (data) => {
    return request.request({
      url: "home/user_address/add_update",
      datas: {
        id: "",
        name: data.name,
        telephone: data.telephone,
        province_id: data.province_id,
        city_id: data.city_id,
        district_id: data.district_id,
        location: data.location,
        address: data.address,
        is_default: data.is_default || 0,
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 编辑地址（使用同一个接口）
  updateAddress: (id, data) => {
    return request.request({
      url: "home/user_address/add_update",
      datas: {
        id: id,
        name: data.name,
        telephone: data.telephone,
        province_id: data.province_id,
        city_id: data.city_id,
        district_id: data.district_id,
        location: data.location,
        address: data.address,
        is_default: data.is_default || 0,
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 删除地址
  deleteAddress: (data) => {
    return request.request({
      url: "home/user_address/del_user_address",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取默认地址
  getDefaultAddress: () => {
    return request.request({
      url: "home/user_address/get_default_address",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 获取地址详情
  getAddressDetail: (id) => {
    return request.request({
      url: "home/user_address/get_address_detail",
      datas: {
        id,
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 获取所有地区数据
  getAllRegion: () => {
    return request.request({
      url: "home/common/get_all_region",
      datas: {
        grts: new Date().getTime() / 1000,
      },
      showLog: false,
    });
  },
};

/**
 * 支付相关接口
 */
export const paymentApi = {
  // 获取支付方式
  getPaymentMethods: () => {
    return request.request({
      url: "home/Payment/get_payment_methods",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 创建支付订单
  createPayment: (data) => {
    return request.request({
      url: "home/Payment/create_payment",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 查询支付状态
  getPaymentStatus: (data) => {
    return request.request({
      url: "home/Payment/get_payment_status",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取用户余额
  getUserBalance: () => {
    return request.request({
      url: "home/wallet/user_account",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 检查支付密码是否已设置
  checkPayPassword: () => {
    return request.request({
      url: "home/pay/check_pwd",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 设置支付密码
  setPayPassword: (data) => {
    return request.request({
      url: "home/pay/add_pwd",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 验证支付密码
  verifyPayPassword: (data) => {
    return request.request({
      url: "home/pay/verify_pwd",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 余额支付
  balancePay: (data) => {
    return request.request({
      url: "home/Balance/balancePay",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 微信支付
  wechatPay: (data) => {
    return request.request({
      url: "home/Alipay/AppTrade",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 银联支付
  unionPay: (data) => {
    return request.request({
      url: "wap/union_pay/pay",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },
};

/**
 * 系统相关接口
 */
export const systemApi = {
  // 获取首页数据
  getHomeData: () => {
    return request.request({
      url: "home/home/selHome2",
      datas: {},
      showLog: false,
    });
  },

  // 检查新消息
  checkNewMessage: () => {
    return request.request({
      url: "home/Message/check_new_message",
      datas: {},
      token: getToken(),
      showLog: false,
    });
  },

  // 获取搜索建议
  getSearchSuggestions: (keyword) => {
    return request.request({
      url: "home/Search/get_suggestions",
      datas: {
        keyword,
      },
      showLog: false,
    });
  },

  // 获取地区列表
  getRegionList: () => {
    return request.request({
      url: "home/Region/get_region_list",
      datas: {},
      showLog: false,
    });
  },

  // 获取轮播图
  getBannerList: () => {
    return request.request({
      url: "home/Index/get_banner_list",
      datas: {},
      showLog: false,
    });
  },

  // 获取公告
  getNoticeList: () => {
    return request.request({
      url: "home/notice/get_notice_all",
      datas: {},
      showLog: false,
    });
  },

  // 获取通知详细列表
  getNoticeDetailList: (data) => {
    return request.request({
      url: "home/notice/get_notice_all",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取客服电话
  getServicePhone: () => {
    return request.request({
      url: "home/service/getServicePhone",
      datas: {},
      showLog: false,
    });
  },

  // 意见反馈
  submitFeedback: (data) => {
    return request.request({
      url: "home/Feedback/submit_feedback",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 提交意见反馈
  addFeedback: (data) => {
    return request.request({
      url: "home/feedback/add",
      datas: {
        type: data.type,
        content: data.content
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 获取反馈列表
  getFeedbackList: (data) => {
    return request.request({
      url: "home/feedback/list",
      datas: {
        page: data.page || 1
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 标记反馈为已读
  readFeedback: (data) => {
    return request.request({
      url: "home/feedback/read",
      datas: {
        id: data.id
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 获取版本信息
  getVersionInfo: () => {
    return request.request({
      url: "home/System/get_version_info",
      datas: {},
      showLog: false,
    });
  },

  // 获取未登录用户信息
  getUnlogInfo: () => {
    return request.request({
      url: "home/User/unloginfo",
      datas: {},
      showLog: false,
    });
  },

  // 获取系统配置
  getConfig: () => {
    return request.request({
      url: "home/System/get_config",
      datas: {},
      showLog: false,
    });
  },

  // 获取轮播图列表
  getBanners: () => {
    return request.request({
      url: "home/System/get_banners",
      datas: {},
      showLog: false,
    });
  },

  // 获取通知列表
  getNotices: () => {
    return request.request({
      url: "home/home/getNotices",
      datas: {},
      showLog: false,
    });
  },

  // 获取为您推荐商品
  getCateGoods: (params) => {
    return request.request({
      url: "home/home/cateGoods",
      datas: params || {},
      showLog: false,
    });
  },

  // 获取专区主题信息
  getThemeInfo: (data) => {
    return request.request({
      url: "home/home/theme",
      method: "POST",
      datas: data,
      showLog: false,
    });
  },

  // 获取专区商品列表
  getThemeGoodsList: (data) => {
    return request.request({
      url: "home/home/themeGoodsList",
      method: "POST",
      datas: data,
      showLog: false,
    });
  },
};

/**
 * 分润相关接口
 */
export const profitApi = {
  // 获取收益信息
  getIncome: (page = 1) => {
    return request.request({
      url: "home/User/income",
      datas: {
        page,
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 获取分润记录
  getProfitRecord: (params) => {
    return request.request({
      url: "home/User_income/getProfitRecord",
      datas: params,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取待分润金额
  getPendingProfit: () => {
    return request.request({
      url: "home/User_income/getPendingProfit",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 获取已分润金额
  getCompletedProfit: () => {
    return request.request({
      url: "home/User_income/getCompletedProfit",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 获取分润列表
  getProfitList: (params) => {
    return request.request({
      url: "home/User_income/getProfitList",
      datas: params,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取分润统计
  getProfitStats: () => {
    return request.request({
      url: "home/User_income/getProfitStats",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 提现申请
  withdraw: (data) => {
    return request.request({
      url: "home/User_income/withdraw",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取今日分润
  getTodayProfit: () => {
    return request.request({
      url: "home/User_income/getTodayProfit",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },
};

/**
 * 团队相关接口
 */
export const teamApi = {
  // 获取团队收益
  getTeamProfit: () => {
    return request.request({
      url: "home/Team/teamProfit",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },
  // 获取云主办公
  getCloudWork: () => {
    return request.request({
      url: "home/Team/cloudWork",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },
  // 获取市运营办公
  getAgentWork: () => {
    return request.request({
      url: "home/Team/agentWork",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },
  // 获取团队信息
  getTeamInfo: () => {
    return request.request({
      url: "home/Team/getTeamInfo",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 获取团队成员列表
  getTeamMembers: (params) => {
    return request.request({
      url: "home/Team/getTeamMembers",
      datas: params,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取邀请码
  getInviteCode: () => {
    return request.request({
      url: "home/User/getInviteCode",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 生成邀请二维码
  generateInviteQrcode: () => {
    return request.request({
      url: "home/User/generateInviteQrcode",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 获取任务历史
  getTaskHistory: (data) => {
    const url = data.userType === 1 ? "home/Team/cloudHistory" : "home/Team/agentHistory";
    return request.request({
      url: url,
      datas: {
        y: data.y,
        m: data.m,
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 获取云主成长计划
  getCloudGrowPlan: () => {
    return request.request({
      url: "home/Team/cloudGrowPlan",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 获取代理人成长计划
  getAgentGrowPlan: () => {
    return request.request({
      url: "home/Team/agentGrowPlan",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 获取月度统计数据
  getMonthlyStats: (data) => {
    return request.request({
      url: "home/Team/monthStats",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取区域列表
  getAreaList: () => {
    return request.request({
      url: "home/Team/getArea",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 获取团队排名/贡献列表
  getTeamRanking: (data) => {
    return request.request({
      url: "home/Team/teamRanking",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },
};

/**
 * O2O本地生活接口
 */
export const o2oApi = {
  // 获取O2O分类列表
  getCategoryList: () => {
    return request.request({
      url: "o2o/merch_group_category/getcatelist",
      datas: {},
      showLog: false,
	  client:2,
    });
  },

  // 获取顶级分类列表（原系统接口）
  getTopCateList: () => {
    return request.request({
      url: "o2o/merch_group_category/getTopCateList",
      datas: {},
      showLog: false,
	  client:2,
    });
  },

  // 获取为你推荐商品（原系统接口）
  forYouPush: (params) => {
    return request.request({
      url: "o2o/index/forYouPush",
      datas: params || {},
      showLog: true,
	  client:2,
    });
  },

  // 获取O2O商家列表
  getMerchantList: (params) => {
    return request.request({
      url: "o2o/merchant/getList",
      datas: params,
      showLog: false,
	  client:2,
    });
  },

  // 获取O2O商品列表
  getGoodsList: (params) => {
    return request.request({
      url: "o2o/goods/getList",
      datas: params,
      showLog: false,
	  client:2,
    });
  },

  // 获取O2O商品详情
  getGoodsDetail: (id) => {
    return request.request({
      url: "o2o/goods/detail",
      datas: {
        id,
      },
      showLog: false,
	  client:2,
    });
  },

  // 提交O2O订单
  submitOrder: (data) => {
    return request.request({
      url: "o2o/order/submit",
      datas: data,
      token: getToken(),
      showLog: true,
	  client:2,
    });
  },

  // 获取O2O订单列表
  getOrderList: (params) => {
    return request.request({
      url: "o2o/order/getList",
      datas: params,
      token: getToken(),
      showLog: true,
	  client:2,
    });
  },

  // 获取分类子级列表
  getCateChild: (params) => {
    return request.request({
      url: "o2o/merch_group_category/getCateChild",
      datas: params,
      showLog: false,
	  client:2,
    });
  },

  // 获取商家店铺信息（通过store_code）
  getStoreCodeData: (data) => {
    return request.request({
      url: "home/wx_table_card/getStoreCodeData",
      datas: data,
      showLog: true,
	  client:2,
    });
  },

  // 获取微信授权用户信息
  getCodeOpenid: (data) => {
    return request.request({
      url: "home/wx_oauth/get_code_openid",
      datas: data,
      showLog: true,
    });
  },

  // 扫码支付接口
  nativePay: (data) => {
    return request.request({
      url: "home/wx_table_card/nativepay",
      datas: data,
      showLog: true,
    });
  },

  // 搜索团购商品
  searchGroupGoods: (params) => {
    return request.request({
      url: "o2o/Search/searchGroupGoods",
      datas: params,
      showLog: true,
	  client:2,
    });
  },

  // 获取附近区域列表
  // 根据城市名称获取附近区域列表
  getVicinity: (params) => {
    return request.request({
      url: "o2o/index/getVicinity",
      datas: params,
      showLog: false,
	  client:2,
    });
  },

  // 获取分类列表（用于筛选）
  getCateList: () => {
    return request.request({
      url: "o2o/merch_group_category/getcatelist",
      datas: {},
      showLog: false,
	  client:2,
    });
  },

  // 获取商品列表（生活服务页面专用）
  getGoodsListForLife: (params) => {
    return request.request({
      url: "o2o/index/getGoodsList",
      datas: params,
      showLog: false,
	  client:2,
    });
  },

  // 获取商家详情信息
  getMerchantInfo: (merch_id) => {
    return request.request({
      url: "o2o/merchant_info/get_merch_info",
      datas: {
        merch_id: merch_id,
      },
      showLog: true,
	  client:2,
    });
  },

  // 获取商家商品列表
  getStoreGoods: (store_id, page = 1) => {
    return request.request({
      url: "o2o/merchant_info/getStoreGoods",
      datas: {
        store_id: store_id,
        page: page,
      },
      showLog: true,
	  client:2,
    });
  },

  // 获取团购商品详情
  getGroupDetail: (params) => {
    return request.request({
      url: "o2o/group/get_detail",
      datas: params,
      showLog: true,
	  client:2,
    });
  },

  // 获取图文详情
  getImageTextDetail: (params) => {
    return request.request({
      url: "o2o/group/getImageTextDetail",
      datas: params,
      showLog: true,
	  client:2,
    });
  },
};

/**
 * 投资管理相关接口
 */
export const investmentApi = {
  // 获取团队列表
  getGroupList: () => {
    return request.request({
      url: "home/mnangement/group_list",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 添加团队成员
  addTeamMember: (data) => {
    return request.request({
      url: "home/mnangement/add_member",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取成员列表
  getMemberList: (groupId) => {
    return request.request({
      url: "home/mnangement/member_list",
      datas: {
        group_id: groupId,
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 删除团队成员
  deleteMember: (memberId) => {
    return request.request({
      url: "home/mnangement/delete_member",
      datas: {
        member_id: memberId,
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 编辑团队成员
  editMember: (data) => {
    return request.request({
      url: "home/mnangement/edit_member",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取招商管理团队列表
  getMerchantTeamList: (data) => {
    return request.request({
      url: "home/merchant/team_list",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取招商管理成员列表
  getMerchantMemberList: (data) => {
    return request.request({
      url: "home/merchant/member_list",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取招商管理业绩列表
  getMerchantPerformanceList: (data) => {
    return request.request({
      url: "home/merchant/performance_list",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 创建新团队
  createMerchantTeam: (data) => {
    return request.request({
      url: "home/merchant/create_team",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 编辑团队
  editMerchantTeam: (data) => {
    return request.request({
      url: "home/merchant/edit_team",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 删除团队
  deleteMerchantTeam: (teamId) => {
    return request.request({
      url: "home/merchant/delete_team",
      datas: {
        team_id: teamId,
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 编辑成员
  editMerchantMember: (data) => {
    return request.request({
      url: "home/merchant/edit_member",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 删除成员
  deleteMerchantMember: (memberId) => {
    return request.request({
      url: "home/merchant/delete_member",
      datas: {
        member_id: memberId,
      },
      token: getToken(),
      showLog: true,
    });
  },
//获取评价列表
  getEvalList: (params) => {
    return request.request({
      url: "o2o/group_eval/get_eval_list",
      datas: params,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取支付订单信息
  getPayOrder: (params) => {
    return request.request({
      url: "o2o/group/payOrder",
      datas: params,
      token: getToken(),
      showLog: true,
    });
  },

  // 购买团购商品
  buyGroup: (params) => {
    return request.request({
      url: "o2o/group/buy_group",
      datas: params,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取团购订单详情
  getGroupOrderInfo: (params) => {
    return request.request({
      url: "o2o/my_order/groupOrderInfo",
      datas: params,
      token: getToken(),
      showLog: true,
    });
  },

  // 提交团购评价
  submitGroupComment: (params) => {
    return request.request({
      url: "o2o/group_eval/go_group_eval",
      datas: params,
      token: getToken(),
      showLog: true,
    });
  },
};

/**
 * 身份认证接口
 */
export const identityApi = {
	// 获取身份认证信息
	getIdentityInfo: () => {
	  return request.request({
	    url: "home/Apply/get_identity_info",
	    datas: {},
	    token: getToken(),
	    showLog: true,
	  });
	},
	
	// 提交身份认证（实名认证并绑定银行卡）
	submitIdentity: (data, urlType = 'real_identity') => {
	  const url = urlType === 'staff_real_identity' ? "home/Apply/staff_real_identity" : "home/apply/real_identity";
	  return request.request({
	    url: url,
	    datas: {
	      realname: data.realname,
	      id_number: data.id_number,
	      bank_num: data.bank_num,
	      bank_name: data.bank_name
	    },
	    token: getToken(),
	    showLog: true,
	  });
	},
 

  // // 提交身份认证
  // submitIdentity: (data) => {
  //   return request.request({
  //     url: "home/Apply/submitIdentity",
  //     datas: data,
  //     token: getToken(),
  //     showLog: true,
  //   });
  // },

  // 实名认证
  realIdentity: (data) => {
    return request.request({
      url: "home/apply/real_identity",
      datas: {
        realname: data.realname,
        id_number: data.id_number,
        bank_num: data.bank_num,
        bank_name: data.bank_name,
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 员工实名认证
  staffRealIdentity: (data) => {
    return request.request({
      url: "home/Apply/staff_real_identity",
      datas: {
        realname: data.realname,
        id_number: data.id_number,
        bank_num: data.bank_num || "",
        bank_name: data.bank_name || "",
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 申请合伙人
  applyPartner: (data) => {
    return request.request({
      url: "home/Apply/applyPartner",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取员工申请列表
  getStaffApplyList: (data) => {
    return request.request({
      url: "home/staff_apply/list",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 新增员工申请
  addStaffApply: (data) => {
    return request.request({
      url: "home/staff_apply/add",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 更新员工申请
  updateStaffApply: (data) => {
    return request.request({
      url: "home/staff_apply/update",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取员工数据（云主申请页面使用）
  getStaffData: () => {
    return request.request({
      url: "home/apply/staffData",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 申请云主身份（创建订单）
  applyCloudWorker: (data) => {
    return request.request({
      url: "home/apply/apply_pay",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 添加保单信息
  addInsuranceInfo: (data) => {
    return request.request({
      url: "home/Apply/add_insurance_info",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },
};

/**
 * 市运营代理相关接口
 */
export const agentApi = {
  // 获取身份认证信息
  getIdentityInfo: () => {
    return request.request({
      url: "home/Apply/get_identity_info",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 获取所有地区数据
  getAllRegion: () => {
    return request.request({
      url: "home/common/get_all_region",
      datas: {
        grts: new Date().getTime() / 1000,
      },
      showLog: false,
    });
  },

  // 获取代理申请列表
  getAgentList: (data) => {
    return request.request({
      url: "home/agent_apply/aalist",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 新增市运营代理申请
  addAgentInfo: (data) => {
    return request.request({
      url: "home/agent_apply/add_agent_info",
      datas: {
        region: data.region,
        realname: data.realname,
        id_number: data.id_number,
        cfi_path: data.cfi_path,
        cbi_path: data.cbi_path,
        st_path: data.st_path,
        ws_path: data.ws_path,
        tp_path: data.tp_path,
        created: data.created,
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 更新市运营代理申请
  updateAgentInfo: (data) => {
    return request.request({
      url: "home/agent_apply/add",
      datas: {
        agent_id: data.agent_id,
        order_num: data.order_num || "",
        region: data.region,
        realname: data.realname,
        id_number: data.id_number,
        cfi_path: data.cfi_path,
        cbi_path: data.cbi_path,
        st_path: data.st_path,
        ws_path: data.ws_path,
        tp_path: data.tp_path,
        created: data.created,
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 上传图片
  uploadImage: (data) => {
    return new Promise((resolve, reject) => {
      uni.uploadFile({
        url: request.defaults.baseUrl + "home/upload/upload_img_wx",
        filePath: data.filePath,
        name: data.name || "file",
        header: {
          token: getToken(),
        },
        success: (uploadRes) => {
          try {
            const result = JSON.parse(uploadRes.data);
            resolve(result);
          } catch (error) {
            reject(error);
          }
        },
        fail: (error) => {
          reject(error);
        },
      });
    });
  },
};

/**
 * 申请相关接口
 */
export const applyApi = {
  // 获取更多申请列表
  getMoreApply: () => {
    return request.request({
      url: "home/Apply/more_apply",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 获取申请历史
  getApplyHistory: () => {
    return request.request({
      url: "home/Apply/get_apply_history",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 获取申请明细（合伙明细）
  getApplyDetail: (data) => {
    return request.request({
      url: "home/Apply/get_apply_detail",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 检查市运营申请
  checkAgentApply: () => {
    return request.request({
      url: "home/agent_apply/check_agent_apply",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 获取员工申请信息
  getStaffApply: () => {
    return request.request({
      url: "home/apply/staff_apply",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 获取身份认证信息
  getIdentityInfo: () => {
    return request.request({
      url: "home/Apply/get_identity_info",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 获取保单信息
  getPolicyInfo: (data) => {
    return request.request({
      url: "home/policy/get_policy_info",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 检查支付密码是否已设置
  checkPayPassword: () => {
    return request.request({
      url: "home/pay/check_pwd",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 设置支付密码
  setPayPassword: (data) => {
    return request.request({
      url: "home/pay/add_pwd",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 验证支付密码
  verifyPayPassword: (data) => {
    return request.request({
      url: "home/pay/verify_pwd",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 退出市运营
  exitAgent: (data) => {
    return request.request({
      url: "home/agent_apply/exit_agent",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },
};

/**
 * 钱包相关接口
 */
export const walletApi = {
  // 获取用户账户信息
  getUserAccount: () => {
    return request.request({
      url: "home/wallet/user_account",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 检查支付密码是否已设置
  checkPayPassword: () => {
    return request.request({
      url: "home/pay/check_pwd",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 设置支付密码
  setPayPassword: (data) => {
    return request.request({
      url: "home/pay/add_pwd",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 验证支付密码
  verifyPayPassword: (data) => {
    return request.request({
      url: "home/pay/verify_pwd",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 支付宝提现
  alipayWithdraw: (data) => {
    return request.request({
      url: "home/Alipay/alipayWithdrawals",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取银行卡列表
  getBankCardList: () => {
    return request.request({
      url: "home/user_card_list/list",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },
  // 获取钱包信息
  getWalletInfo: () => {
    return request.request({
      url: "home/Wallet/getWalletInfo",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 获取余额明细
  getBalanceDetail: (params) => {
    return request.request({
      url: "home/Wallet/getBalanceDetail",
      datas: params,
      token: getToken(),
      showLog: true,
    });
  },

  // 删除银行卡
  deleteBankCard: (id) => {
    return request.request({
      url: "home/user_card_list/del",
      datas: { id },
      token: getToken(),
      showLog: true,
    });
  },

  // 申请提现
  applyWithdraw: (data) => {
    return request.request({
      url: "home/Wallet/applyWithdraw",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取提现记录
  getWithdrawRecord: (params) => {
    return request.request({
      url: "home/Wallet/getWithdrawRecord",
      datas: params,
      token: getToken(),
      showLog: true,
    });
  },
};

/**
 * 银行卡相关接口
 */
export const bankCardApi = {
  // 获取身份认证信息
  getIdentityInfo: () => {
    return request.request({
      url: "home/Apply/get_identity_info",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 添加银行卡
  addBankCard: (data) => {
    return request.request({
      url: "home/user_card_list/add",
      datas: {
        card_num: data.card_num,
        cardholder_name: data.cardholder_name,
        id_number: data.id_number,
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 获取银行卡列表
  getBankCardList: () => {
    return request.request({
      url: "home/user_card_list/list",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 删除银行卡
  deleteBankCard: (data) => {
    return request.request({
      url: "home/user_card_list/delete",
      datas: {
        id: data.id,
      },
      token: getToken(),
      showLog: true,
    });
  },
};

/**
 * 合伙人相关接口
 */
export const partnerApi = {
  // 获取合伙人价格
  getPartnerMoney: () => {
    return request.request({
      url: "home/Partner/getPartnerMoney",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 申请支付（创建订单）
  applyPay: (data) => {
    return request.request({
      url: "home/apply/apply_pay",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 提交代理人资料
  addAgentApply: (data) => {
    return request.request({
      url: "home/agent_apply/add",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },
};

/**
 * 厂家申请相关接口
 */
export const manufacturerApi = {
  // 提交厂家申请
  submitApplication: (data) => {
    return request.request({
      url: "home/manufac_apply/add",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },
};

/**
 * 商家申请相关接口
 */
export const merchantApi = {
  // 获取商家申请列表
  getApplyList: (data) => {
    return request.request({
      url: "home/merchant_apply/aplist",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 提交商家申请第一步（身份验证）
  submitMerchantStep1: (data) => {
    return request.request({
      url: "home/merchant_apply/step1",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 添加保单信息
  addInsuranceInfo: (data) => {
    return request.request({
      url: "home/Apply/add_insurance_info",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },
  // 提交商家申请第一步（商家信息）
  submitBusinessStep1: (data) => {
    return request.request({
      url: "home/merchant_apply/submit_step1",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 提交商家申请第二步（商家信息）
  submitBusinessStep2: (data) => {
    return request.request({
      url: "home/merchant_apply/submit_step2",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 上传图片
  uploadImage: (data) => {
    return request.uploadImage(data);
  },

  // 获取厂家申请列表
  getManufacturerApplyList: (data) => {
    return request.request({
      url: "home/manufacturer_apply/list",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 提交厂家申请第一步
  submitManufacturerStep1: (data) => {
    return request.request({
      url: "home/manufacturer_apply/submit_step1",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 提交厂家申请第二步
  submitManufacturerStep2: (data) => {
    return request.request({
      url: "home/manufacturer_apply/submit_step2",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 提交商家申请第三步（完成申请）
  submitBusinessStep3: (data) => {
    return request.request({
      url: "home/merchant_apply/submit_step3",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取商家申请详情
  getApplyDetail: (data) => {
    return request.request({
      url: "home/merchant_apply/get_detail",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 更新商家申请信息
  updateApplyInfo: (data) => {
    return request.request({
      url: "home/merchant_apply/update_info",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取所有地区数据
  getAllRegion: () => {
    return request.request({
      url: "home/common/get_all_region",
      datas: {
        grts: new Date().getTime() / 1000,
      },
      showLog: false,
    });
  },

  // 获取商家分类
  getMerchCategory: (data) => {
	  
    return request.request({
      url: "home/common/get_merch_cate",
      datas: {
                mts:  Math.floor(new Date().getTime() / 1000)
            },
      showLog: false,
    });
  },

  // 新增商家申请
  addBusinessApply: (data) => {
    return request.request({
      url: "home/merchant_apply/add",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 更新商家申请
  updateBusinessApply: (data) => {
    return request.request({
      url: "home/merchant_apply/update",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 上传图片
  uploadImage: (filePath) => {
    return request.uploadImage({
      filePath: filePath,
      name: "file",
    });
  },
};

/**
 * 认证相关接口
 */
export const authApi = {
  // 发送短信验证码
  sendSmsCode: (data) => {
    return request.request({
      url: "home/index/gen_mobile_code",
      datas: {
        mobile: data.phone,
        salt: "1234",
      },
      showLog: false,
    });
  },

  // 短信验证码登录
  smsLogin: (data) => {
    return request.request({
      url: "home/index/login",
      datas: {
        un: data.phone,
        pwd: data.code,
        type: 1,
        last_login_ip: "0.0.0.0",
        latitude: 0,
        longitude: 0,
      },
      showLog: false,
    });
  },

  // 密码登录
  passwordLogin: (data) => {
    return request.request({
      url: "home/index/login",
      datas: {
        un: data.phone,
        pwd: data.password,
        type: 2,
        last_login_ip: "0.0.0.0",
        latitude: 0,
        longitude: 0,
      },
      showLog: false,
    });
  },

  // 用户登录
  login: (data) => {
    return request.request({
      url: "home/index/login",
      datas: data,
      showLog: false,
    });
  },

  // 检查手机号是否已被使用
  checkMobile: (data) => {
    return request.request({
      url: "home/index/check_mobile",
      datas: {
        mobile: data.phone,
        last_login_ip: "0.0.0.0",
        salt: "1234",
        type: 1,
      },
      showLog: false,
    });
  },

  // 验证邀请码
  checkInviteCode: (data) => {
    return request.request({
      url: "home/index/region_by_invite_code",
      datas: {
        invite_code: data.inviteCode,
      },
      showLog: false,
    });
  },

  // 验证手机验证码
  checkMobileCode: (data) => {
    return request.request({
      url: "home/index/check_mobile_code",
      datas: {
        mobile: data.phone,
        code: data.smsCode,
        last_login_ip: "0.0.0.0",
        salt: "1234",
      },
      showLog: false,
    });
  },

  // 重置密码
  resetPassword: (data) => {
    return request.request({
      url: "home/index/reset_password",
      datas: data,
      showLog: false,
    });
  },
};

/**
 * 论坛相关接口
 */
export const forumApi = {
  // 获取优享优品讲堂列表
  getForumChannel: () => {
    return request.request({
      url: "forum/forum_channel/get_forum_channel",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 获取论坛列表
  getForumList: (data) => {
    return request.request({
      url: "forum/forum_channel/get_forum_list",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取论坛详情
  getForumDetail: (data) => {
    return request.request({
      url: "forum/forum_channel/get_forum_detail",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 获取课程内容详情
  getForumContent: (data) => {
    return request.request({
      url: "forum/forum_channel/get_forum_content",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },
};

/**
 * 搜索相关接口
 */
export const searchApi = {
  // 搜索商品
  searchGoods: (data) => {
    const time = new Date().getTime();
    return request.request({
      url: `home/search/searchGoods?t=${time}`,
      datas: {
        name: data.keyword || data.name || "",
        zt: data.sort_type || data.zt || "score_total", // 排序类型：score_total(综合), sales_num(销量), sale_price(价格), rebate_money(分润)
        pages: data.page || data.pages || 1,
        order: data.order || 1, // 1:升序, 2:降序
        sale_price: data.price_range || data.sale_price || "0,999999999999", // 价格区间
        is_new: data.is_new || 0, // 是否新品：0否，1是
        brand: data.brand_ids || data.brand || "", // 品牌ID，多个用逗号分隔
        category_id: data.category_ids || data.category_id || "", // 分类ID
        min_price: data.min_price || "",
        max_price: data.max_price || "",
        limit: data.limit || 20,
      },
      showLog: false,
    });
  },

  // 获取搜索建议
  getSuggest: (data) => {
    return request.request({
      url: "home/search/getSuggest",
      datas: {
        keyword: data.keyword || "",
      },
      showLog: false,
    });
  },

  // 获取热门搜索关键词
  getHotKeywords: () => {
    return request.request({
      url: "home/search/getHotKeywords",
      datas: {},
      showLog: false,
    });
  },

  // 获取搜索历史
  getSearchHistory: () => {
    return request.request({
      url: "home/search/getSearchHistory",
      datas: {},
      token: getToken(),
      showLog: false,
    });
  },

  // 保存搜索历史
  saveSearchHistory: (data) => {
    return request.request({
      url: "home/search/saveSearchHistory",
      datas: {
        keyword: data.keyword || "",
      },
      token: getToken(),
      showLog: false,
    });
  },

  // 清空搜索历史
  clearSearchHistory: () => {
    return request.request({
      url: "home/search/clearSearchHistory",
      datas: {},
      token: getToken(),
      showLog: false,
    });
  },
};

/**
 * 品牌相关接口
 */
export const brandApi = {
  // 获取品牌列表
  getBrandList: (data = {}) => {
    return request.request({
      url: "home/brand/getBrandList",
      datas: {
        page: data.page || 1,
        limit: data.limit || 50,
      },
      showLog: false,
    });
  },

  // 获取品牌详情
  getBrandDetail: (data) => {
    return request.request({
      url: "home/brand/getBrandDetail",
      datas: {
        brand_id: data.brand_id || data.id,
      },
      showLog: false,
    });
  },
};

/**
 * 分类相关接口
 */
export const categoryApi = {
  // 获取分类列表
  getCategoryList: (data = {}) => {
    return request.request({
      url: "home/category/getCategoryList",
      datas: {
        parent_id: data.parent_id || 0,
        level: data.level || 1,
      },
      showLog: false,
    });
  },

  // 获取分类详情
  getCategoryDetail: (data) => {
    return request.request({
      url: "home/category/getCategoryDetail",
      datas: {
        category_id: data.category_id || data.id,
      },
      showLog: false,
    });
  },
};

/**
 * 收藏相关接口
 */
export const favoriteApi = {
  // 添加收藏
  addFavorite: (data) => {
    return request.request({
      url: "home/favorite/addFavorite",
      datas: {
        goods_id: data.goods_id,
        type: data.type || 1, // 1:商品收藏
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 取消收藏
  removeFavorite: (data) => {
    return request.request({
      url: "home/favorite/removeFavorite",
      datas: {
        goods_id: data.goods_id,
        type: data.type || 1,
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 获取收藏列表
  getFavoriteList: (data = {}) => {
    return request.request({
      url: "home/favorite/getFavoriteList",
      datas: {
        page: data.page || 1,
        limit: data.limit || 20,
        type: data.type || 1,
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 检查是否已收藏
  checkFavorite: (data) => {
    return request.request({
      url: "home/favorite/checkFavorite",
      datas: {
        goods_id: data.goods_id,
        type: data.type || 1,
      },
      token: getToken(),
      showLog: false,
    });
  },
};

/**
 * 分享相关接口
 */
export const shareApi = {
  // 获取分享模板列表
  getTpls: () => {
    return request.request({
      url: "home/Share/getTpls",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 获取分享链接
  getShareUrl: () => {
    return request.request({
      url: "home/Share/getShareUrl",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 记录分享行为
  recordShare: (data) => {
    return request.request({
      url: "home/Share/recordShare",
      datas: {
        type: data.type || 1, // 分享类型：1-邀请分享，2-商品分享
        content: data.content || "",
        platform: data.platform || "wechat",
      },
      token: getToken(),
      showLog: true,
    });
  },
};

/**
 * 生活服务相关接口
 */
export const lifeApi = {
  // 话费充值
  phoneRecharge: (data) => {
    return request.request({
      url: "home/Life/phone_recharge",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 流量充值
  flowRecharge: (data) => {
    return request.request({
      url: "home/Life/flow_recharge",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 游戏充值
  gameRecharge: (data) => {
    return request.request({
      url: "home/Life/game_recharge",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 生活缴费
  lifePay: (data) => {
    return request.request({
      url: "home/Life/life_pay",
      datas: data,
      token: getToken(),
      showLog: true,
    });
  },

  // 电影票
  movieTicket: (params) => {
    return request.request({
      url: "home/Life/movie_ticket",
      datas: params,
      showLog: false,
    });
  },

  // 火车票
  trainTicket: (params) => {
    return request.request({
      url: "home/Life/train_ticket",
      datas: params,
      showLog: false,
    });
  },

  // 酒店预订
  hotelBooking: (params) => {
    return request.request({
      url: "home/Life/hotel_booking",
      datas: params,
      showLog: false,
    });
  },
};

/**
 * 个人信息相关接口
 */
export const profileApi = {
  // 获取用户详细信息
  getUserDetail: () => {
    return request.request({
      url: "home/user_detail/get_user_detail",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 更新用户详细信息
  updateUserDetail: (data) => {
    const userInfo = getCurrentUser();
    return request.request({
      url: "home/user_detail/update_user_detail_info",
      datas: {
        user_id: userInfo.user_id,
        nickname: data.nickname,
        sex: data.sex,
        birthday: data.birthday,
        file_path: data.file_path,
      },
      token: getToken(),
      showLog: true,
    });
  },

  // 上传图片
  uploadImage: (filePath) => {
    return new Promise((resolve, reject) => {
      uni.uploadFile({
        url: "https://api.youpin91.com/home/Upload/upload_img_wx",
        filePath: filePath,
        name: "file",
        formData: {
          data_base64: "",
        },
        success: (uploadRes) => {
          try {
            const result = JSON.parse(uploadRes.data);
            resolve(result);
          } catch (error) {
            reject(error);
          }
        },
        fail: (error) => {
          reject(error);
        },
      });
    });
  },
};

/**
 * 设置相关接口
 */
export const settingsApi = {
  // 获取用户设置信息
  getUserSettings: () => {
    return request.request({
      url: "home/user_detail/get_user_set",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 获取实名认证状态
  getIdentityStatus: () => {
    return request.request({
      url: "home/Apply/get_identity_info",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 清除缓存
  clearCache: () => {
    return request.request({
      url: "home/system/clear_cache",
      datas: {},
      token: getToken(),
      showLog: true,
    });
  },

  // 获取客服电话
  getServicePhone: () => {
    return request.request({
      url: "home/service/getServicePhone",
      datas: {},
      showLog: false,
    });
  },

  // 获取应用版本信息
  getVersionInfo: () => {
    return request.request({
      url: "home/system/get_version_info",
      datas: {},
      showLog: false,
    });
  },
};

// 导出所有API
export default {
  userApi,
  goodsApi,
  cartApi,
  orderApi,
  addressApi,
  paymentApi,
  systemApi,
  profitApi,
  teamApi,
  o2oApi,
  identityApi,
  walletApi,
  authApi,
  forumApi,
  shareApi,
  lifeApi,
  wechatApi,
  searchApi,
  brandApi,
  categoryApi,
  favoriteApi,
  profileApi,
  settingsApi,
  request: request.request,
  uploadImage: request.uploadImage,
  // 兼容旧版本
  system: systemApi,
  goods: goodsApi,
  profit: profitApi,
  life: lifeApi,
  wechat: wechatApi,
  forum: forumApi,
  search: searchApi,
  brand: brandApi,
  category: categoryApi,
  favorite: favoriteApi,
  profile: profileApi,
  settings: settingsApi,
};
